from django.db import models
from map.models import Map


class Monster(models.Model):
    name = models.CharField(max_length=64, verbose_name='怪物名称')
    max_health = models.SmallIntegerField(default=100, verbose_name='最大生命值')
    min_attack = models.SmallIntegerField(default=1, verbose_name='最小攻击')
    max_attack = models.SmallIntegerField(default=3, verbose_name='最大攻击')
    defense = models.SmallIntegerField(default=1, verbose_name='防御值')
    agility = models.SmallIntegerField(default=1, verbose_name='敏捷')
    momentum = models.SmallIntegerField(default=1, verbose_name='士气')
    location = models.CharField(max_length=64, verbose_name='所在地')
    city = models.CharField(max_length=64,
                            choices=[(city.name, city.name) for city in Map.objects.all()],
                            verbose_name='所在城市')
    bio = models.TextField(max_length=1024, default="", verbose_name='怪物简介')

    class Meta:
        verbose_name = '怪物'
        verbose_name_plural = verbose_name
        ordering = ['name']

    def __str__(self):
        return self.name

    def save(self, *args, **kwargs):
        city_places = Map.objects.get(name=self.city).places
        if self.location in city_places:
            super().save(*args, **kwargs)
        else:
            raise ValueError("城市%s没有%s这个地点！" % (self.city, self.location))
